package com.warren.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.response.Result;
import com.warren.system.entity.LoginLog;
import com.warren.system.service.LoginLogService;
import com.warren.system.vo.LoginLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 登录日志表 前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/loginLog")
@Api(value = "登入日志接口" ,tags = "登入日志接口" )
public class LoginLogController {

    @Resource
    private LoginLogService loginLogService;

    @ApiOperation(value = "分页查询登录日志")
    @PostMapping("/findLoginLogPage")
    public Result findLoginLogPage(@RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "7") Integer size,
                                   @RequestBody LoginLogVO loginLogVO){
        Page<LoginLog> page = new Page<>(current,size);
        QueryWrapper<LoginLog> queryWrapper = getWrapper(loginLogVO);
        IPage<LoginLog> filePage =loginLogService.page(page,queryWrapper);

        long total = filePage.getTotal();
        List<LoginLog> records = filePage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    @ControllerEndpoint(exceptionMessage = "删除登录日志失败",operation = "删除登录日志")
    @ApiOperation(value = "删除登录日志")
    @PostMapping("/deleteLoginLogById")
    public Result deleteLoginLogById(Long id){
        try {
            loginLogService.removeById(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    @ControllerEndpoint(exceptionMessage = "批量删除登录日志",operation = "批量删除日志")
    @ApiOperation(value = "批量删除登录日志")
    @PostMapping("/deleteLoginLogsByIds")
    public Result deleteLoginLogsBys(String ids){
        String[] idsList = ids.split(",");
        loginLogService.removeByIds(Arrays.asList(idsList));
        return Result.ok().message("批量删除成功");
    }



    private QueryWrapper<LoginLog> getWrapper(LoginLogVO loginLogVO){
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        if (loginLogVO != null){
            //查询每个条件
            if (!ObjectUtils.isEmpty(loginLogVO.getId())){
                queryWrapper.like("ip",loginLogVO.getId());
            }

            if (!ObjectUtils.isEmpty(loginLogVO.getUsername())){
                queryWrapper.like("username",loginLogVO.getUsername());
            }
        }
        return queryWrapper.orderByDesc("login_time");
    }
}

